//导入数据库模块
const db = require('../db/db')

// 导入加密模块
const bcrypt = require('bcryptjs')

// 登录模块

const User ={
    async login(name,pwd){
        let sqlStr = 'select * from user'
         
      let data = await new Promise((resolve,reject)=>{
        db.query(sqlStr,(err,res)=>{
            if(err) reject(err);
           resolve(res)
        })
      })
    //   for(let i=0;i<data.length;i++){
    //     // console.log(data[i].user == name && bcrypt.compareSync(pwd,data[i].pwd));
    //     let res = bcrypt.compareSync(pwd,data[i].pwd)
    //     if(data[i].user == name && res){
    //        return data = {code:'200',msg:"登录成功"}
    //     // return console.log(23131);
    //     }else {
    //          data={code:'203',msg:'账号或者密码错误!'}
    //         console.log(99999);
    //     }
    //   }
      let results = data.filter(item=>{
        return item.user == name && bcrypt.compareSync(pwd,item.pwd)
      })
      if(results.length > 0){
        data = {code:'200',msg:"登录成功",data:results}
      }else{
        data = {code:'203',msg:'账号或者密码错误!',data:[]}
      }

      return data
       
    },
    
    // 轮播图模块
    async Banner(){
        let sqlStr = 'select * from banner'
        
        let data = await new Promise((resolve,reject)=>{
            db.query(sqlStr,(err,res)=>{
                if(err) reject(err)
                resolve(res)
            })
        })

        return data
    },

    // 获取nav栏数据
    async NavList(){
        let sqlStr = 'select * from nav'

        let data = await new Promise((resolve,reject)=>{
            db.query(sqlStr,(err,res)=>{
                if(err) reject(err)
                resolve(res)
            })
        })

        return data
    },
    async LogIc(pid){
        let sqlStr = 'SELECT a.fnane, b.* FROM fier a,log b WHERE b.pid = a.id and a.id = ?'
        let data = await new Promise((resolve,reject)=>{
            db.query(sqlStr,pid,(err,res)=>{
                if(err) reject(err)
                resolve(res)
            })
        })

        return data
    },

    // 注册模块(添加用户)
    async Register(user,pwd,addre,oid){
        //生成加密的盐
        let salt = bcrypt.genSaltSync(10)
        //加密密码
       let npwd = bcrypt.hashSync(pwd,salt)
        let sqlStr = 'INSERT INTO `user`(user,pwd,addre,oid) VALUES(?,?,?,?)'
        let data = await new Promise((resolve,reject)=>{
            db.query(sqlStr,[user,npwd,addre,oid],(err,res)=>{
                if(err) reject(err)
                resolve(res)
            })
        })

        return data
    },

    // 查询用户名是否存在
    async Username(name){
        let sqlStr = 'SELECT * FROM user WHERE user =?'
        let data = await new Promise((resolve,reject)=>{
            db.query(sqlStr,name,(err,res)=>{
                if(err) reject(err)
                resolve(res)
            })
        })
        return data
    },

    // 用户添加地址
    async Addre(info){
        console.log(info);
        if(info.isDefault == 'true'){
            let sqlStr = 'UPDATE addres SET isDefault = "false" WHERE uid =?'
                await new Promise((resolve,reject)=>{
                     db.query(sqlStr,[info.uid],(err,res)=>{
                        if(err) reject(err)
                            resolve(res)
                        })
            })
        }
        let sqlStr = 'INSERT INTO addres (addres,tel,aname,isDefault,uid) VALUES(?,?,?,?,?)';
        let {addre,tel,aname,isDefault,uid} = info
        let add = [addre,tel,aname,isDefault,uid]
        let data = await new Promise((resolve,reject)=>{
            db.query(sqlStr,add,(err,res)=>{
                if(err) reject(err)
                resolve(res)
            })
        })
        return data
    },

    // 根据id来查询用户对应的地址
    async GetAddre(id){
        let sqlStr = 'SELECT b.* FROM `user` as a,addres as b WHERE a.addre = b.uid AND a.addre=?'
        let data = await new Promise((resolve,reject)=>{
            db.query(sqlStr,id,(err,res)=>{
                if(err) reject(err)
                resolve(res)
            })
        })
        return data
    },
}


// 将模块进行导出
module.exports = User